using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;


namespace dohoa2
{
// Chinh sua 
// Add comment 
// Add commnet to here
// My name is sss
// My name is thanh
    class Cduongtron : Chinhve 
    {
        int r, xc,yc;
        Point dau, cuoi;


       public Cduongtron(Bitmap hinh, Color mn,int x1, int y1, int x2, int y2 )
       {
// Add comment
           name = "duong tron";
            maunen = mn;
            anh = hinh;
            dau = new Point(); dau.X = x1; dau.Y = y1;
            cuoi = new Point(); cuoi.X = x2; cuoi.Y = y2;
            if (Math.Abs(x2 - x1) > Math.Abs(y2 - y1))
            { 
                xc = (x1 + x2) / 2;
                r = Math.Abs(x2 - x1) / 2;
                yc = (y2 - y1 < 0) ? y1 - r : y1 + r;
            }
            else
            {
                yc = (y2 + y1) / 2;
                r = Math.Abs(y2 - y1) / 2;
               
                xc = (x2 - x1 < 0) ? x1 - r : x1 + r;
            }
        }

        public Cduongtron(Bitmap hinh, Color mn, Point mot, Point hai)
        {
            name = "duong tron";
            int x1, x2, y1, y2;
            x1 = mot.X; x2 = hai.X;
            y1 = hai.Y;y2=hai.Y ;
            maunen = mn;
            anh = hinh;
            dau = new Point(); dau = mot;
            cuoi = new Point(); cuoi = hai;
            if (Math.Abs(x2 - x1) > Math.Abs(y2 - y1))
            {
                r = Math.Abs(x2 - x1) / 2;
                

                xc = (x1 + x2) / 2;
             
             
                
                yc = (y2 - y1 < 0) ? y1 - r : y1 + r;
            }
            else
            {
                yc = (y2 + y1) / 2;
                r = Math.Abs(y2 - y1) / 2;

                xc = (x2 - x1 < 0) ? x1 - r : x1 + r;
            }
            r = r - 2;
        }  
        public override  void ve()
        {
             
            
            //bat dau ve
            int x = 0;
            int y = r;
            int p = 1 - r;
            int px = 3;
            int py = 2 - 2 * r;
            Put8Pixel( xc, yc, x, y);
            while (x < y - 1)
            {
                if (p >= 0)
                {
                    p += py;
                    py += 2;
                    y--;
                }
                p += px;
                px += 2;
                x++;
                Put8Pixel( xc, yc, x, y);
            }
        }

        private void Put8Pixel(int xc, int yc, int x, int y)
        {

            try
            {
                anh.SetPixel(xc + x, yc + y, mauve); 
            
                anh.SetPixel(xc + x, yc - y, mauve); 
            
                anh.SetPixel(xc - x, yc + y, mauve); 
          
                anh.SetPixel(xc - x, yc - y, mauve); 
           
                anh.SetPixel(xc + y, yc + x, mauve); 
           
                anh.SetPixel(xc + y, yc - x, mauve); 
            
                anh.SetPixel(xc - y, yc + x, mauve); 
            
                anh.SetPixel(xc - y, yc - x, mauve); }
            catch       {        }
           
        }

        public override void vechon()
        {
            try
            {
                veonho(xc, yc + r);

                veonho(xc, yc - r);

                veonho(xc - r, yc);

                veonho(xc + r, yc);
            }
            catch { }
        
        }


        public override bool kiemtraduocchon(Point p)
        {
           // MessageBox.Show("hihihih");
            if (p.X > xc - r && p.X < xc + r && p.Y > yc - r && p.Y < yc + r)
              return true; 
            else   return false; 
        }

        public override  void tinhtien(Point mot, Point hai) 
        { 
              int dx, dy;
             
           Color mauluu = new Color();
            mauluu = mauve;
            mauve = maunen;
            
                ve();
                if (duocchon == true) bochon();
           
            mauve = mauluu;
            dx = hai.X - mot.X;
            dy = hai.Y - mot.Y;
        
            if ((xc- r + dx - (int)kichthuoconho / 2 >= 0) && ( xc+r + dx + (int)kichthuoconho / 2) <= anh.Width
                && (yc-r+ dy - (int)kichthuoconho / 2) >= 0 && (yc+r + dy + (int)kichthuoconho / 2) <= anh.Height 
                       
            )
            {
                 xc += dx;
                 yc += dy;
            }
           
            {
                ve();
                if (duocchon == true) vechon();
            }
            
        }

        public override void hienthuoctinh() 
        {
            MessageBox.Show("r:" + r.ToString() + " yc:" + yc.ToString() + " xc:" + xc.ToString());
        }
     
    }



}
